package com.zmn.uac.client;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.uac.UacException;
import com.zmn.uuc.common.constant.DubboConsts;
import com.zmn.uuc.common.dro.user.UserBaseDRO;
import com.zmn.uuc.common.dro.user.UserDetailDRO;
import com.zmn.uuc.dubbo.interfaces.user.UserListRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @title: 个人用户查询Dubbo接口
 * @author:Rananquan
 * @Date:2022/6/9 11:46
 */
@Slf4j
@Component
public class UserListClient {

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    UserListRemoteService userListRemoteService;

    /**
     * 根据手机号查询用户信息
     * @param mobile 手机号
     * @param isSensitive 是否脱敏
     * @return
     */
    public List<UserBaseDRO> listUserByMobile(String mobile,Boolean isSensitive){
        log.info("根据手机号查询用户信息入参，mobile：{}，isSensitive：{}",mobile,isSensitive);
        ResponseDTO<List<UserBaseDRO>> userBaseResponse = userListRemoteService.listUserByMobile(mobile, isSensitive);
        if(!userBaseResponse.isSuccess()){
            log.error("根据手机号查询用户信息请求失败，失败原因：{}",userBaseResponse.getMessage());
            throw new UacException(userBaseResponse.getMessage());
        }

        List<UserBaseDRO> userBaseList = userBaseResponse.getData();
        log.info("根据手机号查询用户信息返回response：{}", JSON.toJSONString(userBaseList));

        return userBaseList;
    }


    /**
     * 根据id查询用户详细资料
     * @param userId 用户Id
     * @param isSensitive 是否脱敏
     * @param openId 三方openid
     * @return
     */
    public UserDetailDRO getThirdUserDetailByIdAndOpenId(Long userId,Boolean isSensitive,String openId){
        log.info("根据id查询用户详细资料入参，userId：{}，isSensitive：{}，openId：{}",userId,isSensitive,openId);
        ResponseDTO<UserDetailDRO> userDetailResponse = userListRemoteService.getThirdUserDetailByIdAndOpenId(userId, isSensitive, openId);
        if(!userDetailResponse.isSuccess()){
            log.error("根据id查询用户详细资料请求失败，失败原因：{}",userDetailResponse.getMessage());
            throw new UacException(userDetailResponse.getMessage());
        }

        UserDetailDRO userDetail = userDetailResponse.getData();
        log.info("根据id查询用户详细资料返回response：[{}]",userDetail);

        return userDetail;
    }
}
